//
//  Emoticonswift
//  01-表情键盘 Emotion
//
//  Created by wulei on 15/12/16.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

class EmoticonAttachment: NSTextAttachment {
    var emoticon: Emoticon
    
    func imageText(font: UIFont) -> NSAttributedString {

        // 创建文本附件
//        let attachment = NSTextAttachment()
        // 将图片添加到文本附件中
        image = UIImage(contentsOfFile: emoticon.imagePath)
        
        
        // 提取textView 中字体的线高
        let lineHeight = font.lineHeight
        // 设置附件的线高
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        // 将附件转换成富文本,再将富文本转换成可变富文本
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        // 给可变富文本添加字体大小属性
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return imageText
    }
    
    
    
    
    init(emoticon: Emoticon){
        self.emoticon = emoticon
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
